package edu.gmu.swe632.fruit.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import edu.gmu.swe632.fruit.R;
import edu.gmu.swe632.fruit.service.AutoSosService;

public class AutoSosActivity extends Activity
{
	private static final String TAG = "autosos";
	private static final int AUTO_SOS_ID = 1;

	private NotificationManager mNotificationManager;
	private TextView sendingText;
	private Timer timer;
	private int countDown = 10;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.auto_sos);

		sendingText = (TextView) findViewById(R.id.sending);
		restartTimer();

		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {
			public void onClick(View view)
			{
				finish();
			}
		});

		Button send = (Button) findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {
			public void onClick(View view)
			{
				finish();
				Toast.makeText(AutoSosActivity.this, SosActivity.SENDING, Toast.LENGTH_LONG).show();
			}
		});

		mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		AutoSosService.isTriggered = true;

		Log.d(TAG, "onCreate");
	}

	@Override
	protected void onNewIntent(Intent intent)
	{
		super.onNewIntent(intent);

		Log.d(TAG, "onNewIntent");

		Bundle bundle = intent.getExtras();

		if (bundle != null && bundle.getInt(AutoSosService.MOTION) == 1)
		{
			Log.d(TAG, "Please finish...");
			finish();
		}
		else
		{
			AutoSosService.isTriggered = true;
			restartTimer();
		}
	}

	@Override
	protected void onPause()
	{
		super.onPause();

		Log.d(TAG, "onPause");
		AutoSosService.isTriggered = false;
		mNotificationManager.cancel(AUTO_SOS_ID);

		killTimer();
	}

	@Override
	protected void onStop()
	{
		super.onStop();

		Log.d(TAG, "onStop");
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();

		Log.d(TAG, "onDestroy");
	}

	private class LocalTimerTask extends TimerTask
	{
		@Override
		public void run()
		{
			runOnUiThread(new LocalUiRunnable());
		}
	}

	private class LocalUiRunnable implements Runnable
	{
		@Override
		public void run()
		{
			Log.d(TAG, "Count down " + countDown);

			if (countDown > 0 && AutoSosService.isTriggered)
			{
				Log.d(TAG, "A");
				String sending = "Sending in " + --countDown;

				sendingText.setText(sending);
				sendingText.invalidate();

				makeNotification(sending);
			}
			else if (AutoSosService.isTriggered)
			{
				Log.d(TAG, "B");
				killTimer();
				Toast.makeText(AutoSosActivity.this, SosActivity.SENDING, Toast.LENGTH_LONG).show();
				finish();
			}
			else
			{
				Log.d(TAG, "C");
				killTimer();
			}
		}
	}

	private void restartTimer()
	{
		timer = new Timer();
		timer.scheduleAtFixedRate(new LocalTimerTask(), 1000, 1000);
	}

	private void killTimer()
	{
		if (timer != null)
		{
			timer.cancel();
			timer = null;
		}
	}

	private void makeNotification(String sending)
	{
		int icon = R.drawable.sos_icon; // icon from resources
		CharSequence tickerText = sending; // ticker-text
		long when = System.currentTimeMillis(); // notification time
		Context context = getApplicationContext(); // application Context
		CharSequence contentTitle = "Auto SOS"; // message title
		CharSequence contentText = sending; // message text

		Intent notificationIntent = new Intent(this, AutoSosActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

		Notification notification = new Notification(icon, tickerText, when);
		notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS;
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

		mNotificationManager.notify(AUTO_SOS_ID, notification);
	}
}
